/*
 * Copyright (C) 2006  Nicolas Normand
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifndef GRAPH_UNDIRECTED_H_
#define GRAPH_UNDIRECTED_H_

#include "graph.h"

namespace graph
{
	
template <typename V, typename A>
class undirected: public virtual basic<V, A>
{
	public:
	// Constructors & destructor
	undirected() {}
	undirected(int n, const V initializator):basic<V, A>::basic(n, initializator) {}
	virtual ~undirected() {}

	undirected<V, A>& operator=(const undirected<V, A>& g)
	{
		this->vertexes__ = g.vertexes__;
		this->edges__ = g.edges__;
		return *this;
	}

	// simple tests
	virtual bool edge_exist(const std::pair<int, int>& edge_)
	{
		return this->edges__.find(edge_) != this->edges__.end() ||
		       this->edges__.find(std::pair<int, int>(edge_.second, edge_.first)) != this->edges__.end();
	}

	// Advanced accessors
	virtual A& edge(const std::pair<int, int>& edge_)
	{
		if (this->edges__.find(edge_) != this->edges__.end())
			return this->edges__[edge_];
		else
		{
			if (this->edges__.find(std::pair<int, int>(edge_.second, edge_.first)) != this->edges__.end())
				return this->edges__[std::pair<int, int>(edge_.second, edge_.first)];
			else
				throw graph::exception::edge_not_found();
		}
	}


	// Complex accessors
	virtual std::list<int> successors(int vertex)
	{
		std::list<int> ls;

		typename basic<V, A>::edge_t::iterator it = this->edges__.begin();
		while (it != this->edges__.end())
		{
			if (it->first.first == vertex)
				ls.push_back(it->first.second);
			else if (it->first.second == vertex)
				ls.push_back(it->first.first);
			it++;
		}
		return ls;
	}

	virtual std::list<int> predecessors(int vertex)
	{
		return successors(vertex);
	}

	
	// Add operators
	virtual void new_edge(int first, int second, const A data)
	{
		if (this->vertex_exist(first) && this->vertex_exist(second))
		{
			std::pair<int, int> p1(first, second);
			std::pair<int, int> p2(second, first);
			bool b1;
			bool b2;

			b1 = this->edges__.find(p1) == this->edges__.end();
			b2 = this->edges__.find(p2) == this->edges__.end();

			if (b2 && b1) // No edge exist
				this->edges__[std::pair<int, int>(first<second?first:second, first<second?second:first)] = data;
			else if (b2) // only p1 exists
				this->edges__[p1] = data;
			else if (b1) // only p2 exists
				this->edges__[p2] = data;
			else
				throw graph::exception::unknown();
		}
		else
			throw graph::exception::vertex_out_of_range();
	}

	// Erasing operators
	virtual void delete_edge(int first, int second)
	{
		if (this->vertex_exist(first) && this->vertex_exist(second))
		{
			bool erased = false;
			
			typename basic<V, A>::edge_t::iterator it = this->edges__.find(std::pair<int, int>(first, second));
			if (it != this->edges__.end())
			{
				this->edges__.erase(it);
				erased = true;
			}
			if ((it = this->edges__.find(std::pair<int, int>(second, first))) != this->edges__.end())
			{
				this->edges__.erase(it);
				erased = true;
			}

			if (!erased)
				throw graph::exception::edge_not_found();
		}
		else
			throw graph::exception::vertex_out_of_range();
	}
};

} // End of: namespace

#endif

